<template>
  <div class="data-container">
    <div class="tab-panel">
      <dv-border-box-8 :color="[mainColorOpcity, mainColor]">
        <div class="tab-content">
          <div class="title">会员等级占比</div>
          <div id="echarts1"></div>
        </div>
      </dv-border-box-8>
    </div>
    <div class="tab-panel">
      <dv-border-box-8 :color="[mainColorOpcity, mainColor]">
        <div class="tab-content">
          <div class="title">会员分类排行</div>
          <div class="rank-list">
            <div class="rank-content" id="animationContainer" @mouseenter="handleStop" @mouseleave="handleStart">
              <div class="content">
                <div class="item-left">
                  <div class="item" v-for="(item, index) in dataSort" :key="item.key">
                    <div class="name">第{{ index + 1 }}名 {{ item.key }}</div>
                    <div class="line-box">
                      <div class="line" :style="{ width: item.width + '%' }"></div>
                    </div>
                  </div>
                </div>
                <div class="item-right">
                  <div class="item" v-for="item in dataSort" :key="item.key">
                    <p class="total">{{ item.value }}条</p>
                  </div>
                </div>
              </div>
              <div class="content">
                <div class="item-left">
                  <div class="item" v-for="(item, index) in dataSort" :key="item.key">
                    <div class="name">第{{ index + 1 }}名 {{ item.key }}</div>
                    <div class="line-box">
                      <div class="line" :style="{ width: item.width + '%' }"></div>
                    </div>
                  </div>
                </div>
                <div class="item-right">
                  <div class="item" v-for="item in dataSort" :key="item.key">
                    <p class="total">{{ item.value }}条</p>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </dv-border-box-8>
    </div>
  </div>
</template>

<script>
import echarts from 'echarts'
import { apiMemberZb, apiMemberClassRank } from '@/api/industryData/index'

export default {
  name: 'panel2',
  data() {
    return {
      mainColor: '#68cee0',
      mainColorOpcity: 'rgba(104, 206, 224, 0.3)',
      myEcharts: '',
      dataList: [],
      rankList: [],
      option: {},
      dataSort: [],
      animation: true,
    }
  },

  mounted() {
    this.getData()
  },
  methods: {
    // 暂停动画
    handleStop() {
      const animationContainer = document.getElementById('animationContainer')
      animationContainer.style.animationPlayState = 'paused'
    },
    // 继续动画
    handleStart() {
      const animationContainer = document.getElementById('animationContainer')
      animationContainer.style.animationPlayState = 'running'
    },
    // 获取数据
    getData() {
      apiMemberZb().then((res) => {
        const { data } = res
        this.dataList = data
        this.initEcharts()
      })
      apiMemberClassRank().then((res) => {
        for (let key in res.data) {
          this.rankList.push({
            key: key,
            value: res.data[key],
          })
        }
        this.hanldeDataSort()
      })
    },
    // 排行数据
    hanldeDataSort() {
      if (this.rankList && this.rankList.length > 0) {
        const arr = this.rankList.sort((a, b) => {
          return b.value - a.value
        })
        const max = arr[0].value
        arr.forEach((item) => {
          item.width = ((item.value / max) * 100).toFixed(2)
        })
        this.dataSort = arr
      }
    },
    // 初始化图表
    initEcharts() {
      const echartsData = this.dataList.map((item) => {
        return {
          name: item.membership_level,
          value: item.num,
        }
      })
      this.myEcharts = echarts.init(document.getElementById('echarts1'))
      this.option = {
        tooltip: {},
        series: [
          {
            type: 'pie',
            radius: '60%',
            center: ['50%', '50%'],
            color: ['#68cee0', 'rgba(104, 206, 224, 0.8)', 'rgba(104, 206, 224, 0.6)', 'rgba(104, 206, 224, 0.4)', 'rgba(104, 206, 224, 0.2)', 'rgba(255, 255, 255, 0.2)', 'rgba(255, 255, 255, 0.4)', 'rgba(255, 255, 255, 0.6)'],
            data: echartsData,
          },
        ],
      }

      // 使用刚指定的配置项和数据显示图表。
      this.myEcharts.setOption(this.option)
    },
    onResize() {
      if (this.myEcharts) {
        this.myEcharts.resize()
      }
    },
  },
}
</script>
<style lang="scss" scoped>
$bgColor: #060a22;
$mainColor: #68cee0;
$mainColorOpcity: rgba(104, 206, 224, 0.3);
.data-container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  overflow: hidden;
}
#echarts1 {
  width: 100%;
  height: 100%;
}
.title {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 10px;
  .icon {
    cursor: pointer;
  }
}
.tab-panel {
  width: 50%;
  height: 100%;
  .tab-content {
    width: 100%;
    height: 100%;
    padding: 20px;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    overflow: hidden;
  }
}

.rank-list {
  width: 100%;
  height: 100%;
  overflow: hidden;
  &::-webkit-scrollbar {
    width: 0;
  }
  .rank-content {
    cursor: pointer;
    animation: scroll 40s linear infinite 1s;
    animation-fill-mode: forwards;
    @keyframes scroll {
      0% {
        transform: translateY(0);
      }
      100% {
        transform: translateY(-50%);
      }
    }
  }
  .content {
    width: 100%;
    display: flex;
    justify-content: flex-start;
    align-items: flex-start;
  }
  &::-webkit-scrollbar {
    width: 0;
  }
  .item-left {
    flex: 1 1 auto;
    margin-right: 10px;
  }
  .item-right {
    flex: 0 0 auto;
  }
  .item {
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: flex-start;
    height: 45px;
    .name {
      color: #ffffff;
      width: 100%;
    }
    .line-box {
      width: 100%;
      margin-top: 6px;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      .line {
        height: 10px;
        border-radius: 5px;
        background: $mainColor;
      }
    }
    .total {
      margin: 0;
      margin-top: 18px;
      color: $mainColor;
    }
  }
}
</style>
